package it.uniba.di.mas.jpnde.agent;

import java.util.Map;

import it.uniba.di.mas.jpnde.console.ConsoleFactory;
import it.uniba.di.mas.jpnde.console.IConsoleUI;
import jade.core.Agent;
import jade.domain.introspection.AMSSubscriber;
import jade.domain.introspection.BornAgent;
import jade.domain.introspection.DeadAgent;
import jade.domain.introspection.Event;
import jade.domain.introspection.IntrospectionVocabulary;

public class JPNDEAmsSubscriber extends Agent{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected void setup() {
		System.out.println("JPNDE AmsSubscriber:"+getAID().getName()+" is ready.");
		try
			{
			addBehaviour(new JPNDEAmsBehaviour(ConsoleFactory.getConsole()));
			}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public class JPNDEAmsBehaviour extends AMSSubscriber{
		
		private IConsoleUI console=null;
		public JPNDEAmsBehaviour(IConsoleUI console){
			this.console=console;
		}
		@Override
		protected void installHandlers(Map handlers) {
			// TODO Auto-generated method stub
			EventHandler creationHandler=new EventHandler(){

				@Override
				public void handle(Event ev) {
					// TODO Auto-generated method stub
					BornAgent ba=(BornAgent)ev;
					console.appendLogMessage("Born Agent <"+ba.getAgent().getName()+">");
				}
				
			};
			handlers.put(IntrospectionVocabulary.BORNAGENT, creationHandler);
			
			EventHandler terminationHandler=new EventHandler(){

				@Override
				public void handle(Event ev) {
					// TODO Auto-generated method stub
					DeadAgent ba=(DeadAgent)ev;
					console.appendLogMessage("Dead Agent <"+ba.getAgent().getName()+">");
				}
				
			};
			handlers.put(IntrospectionVocabulary.DEADAGENT, terminationHandler);
		}
	
	}
	
}
